<template>
  <el-dialog
    :title="'角色权限'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <!-- <div class="ti">角色信息</div>
    <div class="info">
      <div class="name">店员 :{{ info.roleName }}</div>
      <div class="dev">描述 :{{ info.introduction }}</div>
    </div> -->
    <div class="ti">角色权限</div>
    <div class="rolewrap">
      <div
        class="list"
        v-for="(item, index) in list"
        :key="index"
        style="margin-top:15px;"
      >
        <div class="menu2">
          <el-checkbox v-model="item.check" @change="setALL(item)">{{ item.menuName }}</el-checkbox>
          <div class="menu3">
            <el-checkbox
              v-model="i.check"
              v-for="(i, x) in item.sysMenuList"
              :key="x"
              >{{ i.menuName }}</el-checkbox
            >
          </div>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" >保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      checked: false,
      isdisabled: false,
      dataForm: {
        id: 0
      },
      list:[]
    }
  },
  methods: {
    setALL (e) {
      let isTrue = e.check
      e.sysMenuList.forEach((item, index) => {
        item.check = isTrue
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/admin/role/menu/list?roleId=${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === '200') {
              this.list = data.list
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      let menuList = []
      // console.log(this.list)
      // return
      this.list.forEach((item) => {
        if (item.check) {
          menuList.push(item.menuId)
        }
        item.sysMenuList.forEach((i) => {
          if (i.check) {
            menuList.push(i.menuId)
          }
        })
      })
      this.isdisabled = true
      this.$http({
        url: this.$http.adornUrl(`/admin/role/menu/update`),
        method: 'post',
        data: this.$http.adornData({
          menuIdList: menuList,
          roleId: this.dataForm.id
        })
      }).then(({ data }) => {
        if (data && data.code === '200') {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        } else {
          this.$message.error(data.msg)
        }
        this.isdisabled = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.ti {
  font-size: 16px;
  font-weight: 600;
}
.name {
  margin-left: 10px;
  margin-top: 10px;
}
.dev {
  margin-left: 10px;
  margin-top: 10px;
  margin-bottom: 10px;
}
.list {
  margin-left: 10px;
  display: flex;
  margin-top: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #dcdfe6;
  .menu {
    flex: 1;
  }
  .menu2 {
    flex: 2;
  }
  .menu3 {
    flex: 3;
  }
}
</style>
